package com.sk.sourcecircle.view;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.alibaba.fastjson.JSON;
import com.sk.sourcecircle.R;
import com.sk.sourcecircle.activity.home.activity.DetailsOfConsultationActivity;
import com.sk.sourcecircle.activity.home.activity.NoticeActivity;
import com.sk.sourcecircle.entity.NoticeInfoBean;
import com.sk.sourcecircle.utils.HttpAddress;
import com.sk.sourcecircle.utils.MD5;
import com.sk.sourcecircle.utils.MyUtils;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

/**
 * Created by sk on 2017/12/26.
 */

public class NoticeView extends RelativeLayout implements View.OnClickListener {

    private ViewFlipper viewFlipper;
    private TextView more;
    private Context context;
    private String groupId = "";
    private TextView tv_null;
    private ImageView notice;


    public NoticeView(Context context) {
        this(context, null);
    }

    public NoticeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public NoticeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.layout_notice, this);
        viewFlipper = findViewById(R.id.view_flipper);
        tv_null = findViewById(R.id.tv_null);
        notice = findViewById(R.id.notice);
        more = findViewById(R.id.more);
        more.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.more:
                Intent intent = new Intent(context, NoticeActivity.class);
                intent.putExtra("community_id", groupId);
                context.startActivity(intent);
                break;
        }
    }

    public void getNotice(String community_id) {
        this.groupId = community_id;
        if (groupId != null && groupId.length() > 0) {
            if (groupId.equals("-1")) {
                notice.setImageResource(R.mipmap.app_notice);
            } else {
                notice.setImageResource(R.mipmap.group_notice);
            }

            getGroupNotice(community_id);
        }
    }

    private void getGroupNotice(String community_id) {
        RequestParams requestParams = new RequestParams(HttpAddress.TestIp + HttpAddress.GetNotice);
        if (community_id.equals("-1")) {
            requestParams.addBodyParameter("type", "2");
        } else {
            requestParams.addBodyParameter("type", "1");
        }
        long timeStamp = System.currentTimeMillis() / 1000;
        requestParams.addBodyParameter("timestamp", String.valueOf(timeStamp));
        requestParams.addBodyParameter("sign", MD5.md5(timeStamp + "_yuanquan"));
        requestParams.addBodyParameter("token", MyUtils.getToken(context));
        if (community_id != null && community_id.length() > 0) {
            requestParams.addBodyParameter("community_id", community_id);
        }

        Log.v("getNotice", requestParams.toString());
        x.http().get(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.v("getNotice", result);
                NoticeInfoBean noticeInfoBean = JSON.parseObject(result, NoticeInfoBean.class);
                final List<NoticeInfoBean.DataBean> dataBeanList = noticeInfoBean.getData();

                if (dataBeanList != null && dataBeanList.size() > 0) {
                    tv_null.setVisibility(GONE);
                    for (int i = 0; i < dataBeanList.size(); i++) {
                        View view = LayoutInflater.from(context).inflate(R.layout.item_home_notice, null);
                        TextView notice_content = view.findViewById(R.id.notice_content);
                        notice_content.setText(dataBeanList.get(i).getTitle());
                        final int finalI = i;
                        view.setOnClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(context, DetailsOfConsultationActivity.class);
                                intent.putExtra("id", dataBeanList.get(finalI).getArticle_id());
                                context.startActivity(intent);
                            }
                        });
                        viewFlipper.addView(view);
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.v("getNotice", ex.toString());

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}
